
#include <standard/resourcemanager.h>
#include "helper/terrainselecthelperobject.h"
#include "terrain/terrainobject.h"
#include "terrain/terrainblock.h"

CTerrainSelectHelperGeometry::CTerrainSelectHelperGeometry( CTerrainObject* pTerrainObject ){
	m_pTerrainObject = pTerrainObject;
}
CTerrainSelectHelperGeometry::~CTerrainSelectHelperGeometry(){

}
CVertexBuffer* CTerrainSelectHelperGeometry::GetRenderBuffer(){
	return m_rRenderBuffer.GetEntityData();
}
CIndexBuffer* CTerrainSelectHelperGeometry::GetIndexBuffer(){
	return m_rIndexBuffer.GetEntityData();
}
void CTerrainSelectHelperGeometry::UpdateGeometry( float fLeft, float fTop, float fRight, float fBottom ){
	vector<TV3C1> m_vVertex;
	vector<uint16> _vIndexData;
	color32 _cColor = 0xff00ff00;

	TV3C1 _vert;
	TV3C1_SETS( _vert, fLeft, 0, fTop, _cColor );
	m_vVertex.push_back( _vert );
	TV3C1_SETS( _vert, fRight, 0, fTop, _cColor );
	m_vVertex.push_back( _vert );
	TV3C1_SETS( _vert, fRight, 0, fBottom, _cColor );
	m_vVertex.push_back( _vert );
	TV3C1_SETS( _vert, fLeft, 0, fBottom, _cColor );
	m_vVertex.push_back( _vert );

	_vIndexData.push_back( 0 );
	_vIndexData.push_back( 1 );
	_vIndexData.push_back( 2 );
	_vIndexData.push_back( 3 );


	if( m_rRenderBuffer.IsAttached() ){
		m_rRenderBuffer->UpdateBuffer( m_vVertex.data(), m_vVertex.size() );
	} else{
		m_rRenderBuffer = NEW CVertexBuffer( EVT_VECTOR_COLOR, m_vVertex.data(), m_vVertex.size(), True );
	}

	if( m_rIndexBuffer.IsAttached() ){
		m_rIndexBuffer->UpdateBuffer( _vIndexData.data(), _vIndexData.size() );
	} else{
		m_rIndexBuffer = NEW CIndexBuffer( _vIndexData.data(), _vIndexData.size(), True, sizeof( uint16 ) );
	}
}


///////////////////////////////////////////////////////////////////////////
CTerrainSelectHelperObject::CTerrainSelectHelperObject(){

}
CTerrainSelectHelperObject::~CTerrainSelectHelperObject(){

}
void CTerrainSelectHelperObject::Init( CTerrainObject* pTerrainObject, float fLeft, float fTop, float fRight, float fBottom ){
	m_pTerrainObject = pTerrainObject;

	m_rTerrainHelperGeometry = NEW CTerrainSelectHelperGeometry( pTerrainObject );
	m_rTerrainHelperGeometry->UpdateGeometry( fLeft, fTop, fRight, fBottom );

	m_rRenderItem = NEW CRenderItem( m_vWorldM );
	m_rRenderItem->m_eRenderType = 0;
	m_rRenderItem->m_bDepthWrite = False;
    m_rRenderItem->m_bDepthTest = False;
	m_rRenderItem->m_bCastShadow = False;
	m_rRenderItem->m_bReceiveShadow = False;
	m_rRenderItem->m_bFogEnable = False;
	m_rRenderItem->m_bLightEnable = False;
	m_rRenderItem->m_bLineMode = False;
	m_rRenderItem->m_eBlending = AdditiveBlending;
	m_rRenderItem->SetPrimitiveType( EPT_LINE_LIST );
	CTexture* _pTexture = RESOURCE_GETTEXTURE( "UV_Grid_Sm.jpg" );
	m_rRenderItem->CreateNormalDiffuseBatchData( _pTexture, m_rTerrainHelperGeometry->GetRenderBuffer(), m_rTerrainHelperGeometry->GetIndexBuffer() );

}
void CTerrainSelectHelperObject::UpdateGeometry( float fLeft, float fTop, float fRight, float fBottom ){
	m_rTerrainHelperGeometry->UpdateGeometry( fLeft, fTop, fRight, fBottom );
}
int32 CTerrainSelectHelperObject::GetRenderElementHandles( CRenderElement** vRenderElementHandle, int32 nMaxCount ){
	vRenderElementHandle[0] = m_rRenderItem->m_pRenderElement.GetEntityData();
	return 1;
}
